package day20210127;

import org.junit.Test;

/*
* 2. 两数相加
* */
public class AddTwoNumbers {

    public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
        int count = listNodeToInteger(l1)+listNodeToInteger(l2);
        ListNode resultListNode = null, tempListNode = null;
        resultListNode.val = count % 10 ;
        resultListNode.next = tempListNode ;
        count /= 10 ;
        while (count>0) {
            ListNode listNode = null;
            listNode.val = count % 10 ;
            count /= 10 ;
            tempListNode.next = listNode ;
            tempListNode = listNode ;
        }
        return resultListNode;
    }

    public int listNodeToInteger(ListNode listNode) {
        StringBuilder builder = new StringBuilder();
        if (listNode.next != null) {
            builder.append(listNode.val);
        }
        return Integer.valueOf(String.valueOf(builder.reverse()));
    }
}

class ListNode {
    int val;
    ListNode next;
    ListNode() {}
    ListNode(int val) { this.val = val; }
    ListNode(int val, ListNode next) { this.val = val; this.next = next; }
}
